package com.brayan.webapp.controller.realestate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.joda.time.DateTime;
import org.joda.time.Interval;

import com.brayan.webapp.model.realestate.EnrichedInterval;

@Named("cali")
public class CalendarController {

	@Inject
	private Logger LOG;

	private static final int DAYS_IN_INITIAL_PERIOD = 28;
	private static final int DAYS_IN_EXTRA_PERIOD = 14;
	
	private String jsonData;

	public String getJsonData() {
		return jsonData;
	}

	public void setJsonData(String jsonData) {
		this.jsonData = jsonData;
	}

	/**
	 * Return the initial interval as a JSON object.
	 * @return
	 */
	public String getInitialInterval() {
		DateTime intervalStart = new DateTime();
		DateTime intervalEnd = intervalStart.plusDays(DAYS_IN_INITIAL_PERIOD);
		Interval initialInterval = new Interval(intervalStart, intervalEnd);
		
		Map<String,String> data = new HashMap<String,String>();
		
		ObjectMapper mapper = new ObjectMapper();
		String jsonInterval = null;
		try {
			jsonInterval = mapper.writeValueAsString(initialInterval);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setJsonData(jsonInterval);
		return jsonInterval;
	}
	
	/**
	 * Return the extra interval as a JSON object.
	 * @param startingDate
	 * @return
	 */
//	public String getExtraInterval(DateTime startingDate){						
//		Map<String,String> data = new HashMap<String,String>();	
//				
//        data.put("", cptyParam);
//        data.put("exposure", ""+(rand.nextFloat()*10) );
//        data.put("limit", ""+(rand.nextInt(50)));
//
//        String jsonData = toJSONMapper(data);		
//        return jsonData;
//	}
	
	
	/**
	 * mapp the 
	 * @param data
	 * @return
	 */
//	protected String toJSONMapper(Map<String,String> data){
//		
//		JSONObject dataToJSON = new JSONObject();
//		try{
//	        dataToJSON.put("cpty", data.get("cpty"));
//	        dataToJSON.put("exposure", data.get("exposure"));
//	        dataToJSON.put("limit", data.get("limit"));
//	      }
//	      catch (JSONException e){
//	       //TODO Approriate exception handling
//	        e.printStackTrace();
//	      }
//		setJsonData(dataToJSON);
//		return dataToJSON.toString();
//	}
	
//	public String getExtraInterval(DateTime startingDate, int additonalDays){
//		return new EnrichedInterval();
//	}
	
	protected EnrichedInterval enrichIntervalWithReservationDate(Interval displayInterval){
		EnrichedInterval enrichedInterval = new EnrichedInterval();
		return enrichedInterval;
	}
	
//	protected EnrichedInterval enrichIntervalWithLocalPublicHolidays(Interval displayInterval){
//		EnrichedInterval enrichedInterval = new EnrichedInterval();
//		return enrichedInterval;
//	}

	// public Days getNextMonth(){
	// //Days days = Days.daysBetween(start, end)
	// return null;
	//
	// }
}
